package com.hspedu.reader_;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author DL5O
 * @version 1.0
 */
public class FileReader_ {
    public static void main(String[] args) {

    }

    /**
     * 单个字符读取文件
     */
    @Test
    public void readFile01(){
        String filePath = "D:\\story.txt";
        //1.创建FileReader对象
        FileReader fileReader = null;
        int readLen = 0;
        int ch = ' ';
        try {
            fileReader = new FileReader(filePath);
            while( (ch = fileReader.read()) != -1){
                System.out.print((char) ch);
            }
            System.out.println("读取成功...");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 多字符读取
     */
    @Test
    public void readFile02(){
        String filePath = "D:\\story.txt";
        //1.创建FileReader对象
        FileReader fileReader = null;
        int readLen = 0;
        char[] buf = new char[50];
        try {
            fileReader = new FileReader(filePath);
            while((readLen = fileReader.read(buf)) != -1){
                System.out.print(new String(buf,0,readLen));
            }
            System.out.println("读取成功...");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
